package de.java.shortys.myhome.fuelmonitor.controller;

import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.FUELFILLING_EDIT;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;

import de.java.shortys.myhome.api.controller.AbstractSwingModuleController;
import de.java.shortys.myhome.api.controller.AbstractSwingSubModuleController;
import de.java.shortys.myhome.api.controller.IController;
import de.java.shortys.myhome.api.controller.navigation.NavigationController;
import de.java.shortys.myhome.fuelmonitor.model.IFuelFilling;
import de.java.shortys.myhome.fuelmonitor.validation.FillingValidator;
import de.java.shortys.myhome.fuelmonitor.view.SubModuleViewFillingEdit;
import de.java.shortys.myhome.language.controller.LanguageManager;
import de.java.shortys.utils.format.FormatUtils;

public class SubModuleControllerFillingEdit extends AbstractSwingSubModuleController {

	public static final String ID = "FuelMonitor_FillingEdit";

	private IFuelFilling fuelFilling;

	public SubModuleControllerFillingEdit(AbstractSwingModuleController root, IController parent) {
		super(root, parent, new SubModuleViewFillingEdit());
		initialize();
	}

	private void actionSave(SubModuleViewFillingEdit view) {
		final FillingValidator validator = new FillingValidator(view, fuelFilling);
		if (validator.validate()) {
			FuelMonitorController controller = (FuelMonitorController) getRoot();
			IFuelFilling object = validator.getObject();
			if (object != null) {
				controller.updateFuelFilling(object);
				getView().clear();
				NavigationController.getInstance().jumpToModule(SubModuleControllerOverview.ID);
			}
		}
	}

	@Override
	public boolean allowActivate() {
		return true;
	}

	@Override
	public void collapse() {
		// TODO Auto-generated method stub

	}

	@Override
	public void expand() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getIcon() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getId() {
		return ID;
	}

	@Override
	public String getTitle() {
		return LanguageManager.getInstance(FuelMonitorController.NAME).get(FUELFILLING_EDIT);
	}

	@Override
	public void initialize() {
		SubModuleViewFillingEdit view = (SubModuleViewFillingEdit) getView();
		initializeViewListeners(view);
	}

	private void initializeViewListeners(final SubModuleViewFillingEdit view) {
		view.addSaveListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionSave(view);
			}
		});
		view.addCancelListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				deactivate();
				NavigationController.getInstance().jumpToModule(SubModuleControllerOverview.ID);
			}
		});
	}

	@Override
	public boolean list() {
		return true;
	}

	@Override
	public void onActivate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDeactivate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onInitialize() {
		// TODO Auto-generated method stub
	}

	public void setFuelFilling(IFuelFilling fuelFilling) {
		this.fuelFilling = fuelFilling;
		SubModuleViewFillingEdit view = (SubModuleViewFillingEdit) getView();
		view.setDateFilled(FormatUtils.getCalendarOutput(LanguageManager.getInstance(FuelMonitorController.NAME)
				.getCurrentLocale(), fuelFilling.getDateFilled(), DateFormat.MEDIUM));
		view.setAmount(FormatUtils.getDoubleOutput(LanguageManager.getInstance(FuelMonitorController.NAME)
				.getCurrentLocale(), fuelFilling.getAmount()));
		view.setDistance(FormatUtils.getDoubleOutput(LanguageManager.getInstance(FuelMonitorController.NAME)
				.getCurrentLocale(), fuelFilling.getDistance()));
		view.setCharges(FormatUtils.getDoubleOutput(LanguageManager.getInstance(FuelMonitorController.NAME)
				.getCurrentLocale(), fuelFilling.getCharges()));
	}
}
